package cn.tedu.csmall.passport.controller;

import cn.tedu.csmall.passport.pojo.dto.AdminAndNewDTO;
import cn.tedu.csmall.passport.pojo.dto.AdminLoginDTO;
import cn.tedu.csmall.passport.pojo.vo.AdminListItemVO;
import cn.tedu.csmall.passport.security.LoginPrincipal;
import cn.tedu.csmall.passport.service.IAdminService;
import cn.tedu.csmall.passport.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import javax.validation.Valid;
import java.util.List;

@Api(tags = "01. 用户权限管理")
@Slf4j
@RestController
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    private IAdminService adminService;
    public AdminController(){
        log.debug("创建控制：AdminController");
    }
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(AdminLoginDTO adminLoginDTO){
        log.debug("准备处理【管理员登录】的请求：{}",adminLoginDTO);
        String jwt = adminService.log(adminLoginDTO);
        return JsonResult.ok(jwt);
    }
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/and-new")
    public JsonResult<Void> and(@Valid AdminAndNewDTO adminAndNewDTO,
        @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal ){
        log.debug("开始添加用户参数：{}",adminAndNewDTO);
        log.debug("当事人id:{}",loginPrincipal.getId());
        log.debug("当事人username:{}",loginPrincipal.getUsername());
        adminService.AndNew(adminAndNewDTO);
        return JsonResult.ok();
    }
    @ApiOperation("删除管理员数据")
    @ApiOperationSupport(order=200)
    @PreAuthorize("hasAuthority('/ams/admin/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void>delete(@PathVariable Long id){
        log.debug("开始处理查询管理员请求参数:{}",id);
        adminService.deleteById(id);
        return JsonResult.ok();
    }
    //http://localhost:9081/admins/5/enable
    @ApiOperation("启用管理员账号")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@PathVariable Long id) {
        log.debug("准备处理【启用管理员账号】的请求：id={}", id);
        adminService.setEnable(id);
        return JsonResult.ok();
    }
    //http://localhost:9081/admins/5/disable
    @ApiOperation("禁用管理员账号")
    @ApiOperationSupport(order = 301)
    @ApiImplicitParam(name = "id", value = "管理员id", required = true, dataType = "long")
    @PreAuthorize("hasAuthority('/ams/admin/update')")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(@PathVariable Long id) {
        log.debug("准备处理【禁用管理员账号】的请求：id={}", id);
        adminService.setDisable(id);
        return JsonResult.ok();
    }
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order=400)
    @PreAuthorize("hasAuthority('/ams/admin/read')")
    @GetMapping("")
    public JsonResult<List<AdminListItemVO>>list(){
        log.debug("开始处理查询管理员");
        List<AdminListItemVO>adminListItemVOS=adminService.list();
        return JsonResult.ok(adminListItemVOS);
    }
}
